package com.csi.controller.back;


import cn.hutool.jwt.JWTUtil;
import com.csi.domain.Hotel;
import com.csi.domain.HotelFranchise;
import com.csi.domain.Result;
import com.csi.domain.UserAssess;
import com.csi.service.HotelService;
import com.csi.utils.ReturnResults;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RequestMapping("/hotel")
@RestController
public class HotelController {

    @Autowired
    private HotelService hotelService;

    /**
     * 新增酒店 -酒店表-酒店位置表-酒店信息表
     * @param hotel 酒店对象(里面封装了酒店位置对象)
     * @return 执行结果
     */
    @RequestMapping("/save" )
    public Object save(@RequestBody Hotel hotel){
        int saved = hotelService.save(hotel);
        if (saved > 0){
            return new Result("保存成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("保存失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 新增酒店加盟
     * @param hotelFranchise 酒店加盟对象
     * @return 执行结果
     */
    @RequestMapping("/saveHotelFranchise")
    public Object saveHotelFranchise(@RequestBody HotelFranchise hotelFranchise){
        int saved = hotelService.saveHotelFranchise(hotelFranchise);
        if (saved > 0){
            return new Result("加盟成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("加盟失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询所有酒店
     * @return
     */
    @GetMapping("/findAll")
    public Object findAll() {
        List<Hotel> hotels = hotelService.findAll();
        if(hotels != null) {
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }else{
            return new Result("查询失败", ReturnResults.ACCESS_FAILED,hotels);
        }
    }

    /**
     * 推荐酒店
     * @return
     */
    @GetMapping("/recommendHotel")
    public Object recommendHotel(){

        List<Hotel> hotels = hotelService.recommendHotel();
        if(hotels != null && !hotels.isEmpty()) {
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }else{
            return new Result("查询失败", ReturnResults.ACCESS_FAILED,hotels);
        }
    }

    /**
     * 特价酒店
     * @return
     */
    @GetMapping("/discountedhotel")
    public Object discountedHotel(){
        List<Hotel> hotels=hotelService.discountedhotel();
        if(hotels != null && !hotels.isEmpty()) {
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }else{
            return new Result("查询失败", ReturnResults.ACCESS_FAILED,hotels);
        }
    }

    /**
     * 查找所有酒店名
     * @return 执行结果
     */
    @GetMapping("/findAllHotelName")
    public Object findAllHotelName(){
        List<Hotel> hotels = hotelService.findAllHotelName();
        if (hotels != null){
            return new Result<List<Hotel>>("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("当前无酒店入住", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据用户id查找酒店
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelByUserId")
    public Object findHotelByUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());
        List<Map<String, Object>>  hotels = hotelService.findHotelByUserId(userId);
        if (hotels !=null){
            return new Result<List<Map<String, Object>>>("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("当前无预定酒店！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据用户查找未支付酒店订单
     * 待支付
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelByIdUnpaid")
    public Object findHotelByIdUnpaid(HttpServletRequest request){
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());
        List<Map<String,Object>> hotels = hotelService.findHotelByUserIdUnpaid(userId);
        if (hotels !=null){
            return new Result<List<Map<String, Object>>>("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("暂无待支付订单！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据用户id查找未出行酒店
     * 待出行
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelByIdTravel")
    public Object findHotelByIdTravel(HttpServletRequest request){
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());
        List<Map<String,Object>> hotels = hotelService.findHotelByIdTravel(userId);
        if (hotels !=null){
            return new Result<List<Map<String, Object>>>("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("暂无待出行酒店！", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 待评价
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelByIdAssess")
    public Object findHotelByIdAssess(HttpServletRequest request){
        String token = request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());
        List<Map<String,Object>> hotels = hotelService.findHotelByIdAssess(userId);
        if (hotels !=null){
            return new Result<List<Map<String, Object>>>("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("暂无评价！", ReturnResults.ACCESS_FAILED);
    }


    /**
     *根据id查找酒店名字
     * @param request 请求token
     * @return 执行结果
     */
    @GetMapping("/findHotelName")
    public Object findHotelName(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        String hotelName = hotelService.findHotelName(hotelId);
        if (hotelName != null){
            return new Result<>("查询成功", ReturnResults.ACCESS_SUCCESS,hotelName);
        }
        return new Result<>("查询失败",ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据酒店地址信息模糊查询
     * @param name 酒店地址部分字段
     * @return
     */
    @GetMapping("/likeHotelByAddress")
    public Object likeHotelByAddress(@RequestParam(value = "name",defaultValue="") String name){
        List<Hotel> hotels = hotelService.likeHotelByAddress(name);
        if (hotels != null && !hotels.isEmpty()) {
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 根据城市名称、入住日、离店日、入住人数模糊查询
     * @param peopleNum 入住人数
     * @param city_location 城市名称
     * @param check_time 入住日
     * @param leave_time 离店日
     * @return 返回有空闲房间的酒店信息
     */
    @GetMapping("/like4book")
    public Object like4book(@RequestParam(value="people_num",required=false) Integer peopleNum,
                          @RequestParam(value="city_location",required=false) String city_location,
                          @RequestParam(value="check_time",required=false) String check_time,
                          @RequestParam(value="leave_time",required=false) String leave_time){
        List<Hotel> hotels = hotelService.like4book(peopleNum, city_location, check_time, leave_time);
        if (hotels != null && !hotels.isEmpty()) {
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotels);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询酒店的起步价、评论数量、房型数量
     * @param hotelId
     * @return
     */
    @RequestMapping("/findHotelById")
    public Object findHotelById(@RequestParam(value="hotelId") Long hotelId){
        Map<String, Object> hotel = hotelService.findHotelById(hotelId);
        if (hotel != null){
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotel);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询酒店详情页面内容
     * @param hotelId 酒店id
     * @return Map<String,Object>
     */
    @GetMapping("/findHotelListById")
    public Object findHotelListById(@RequestParam(value = "hotelId") long hotelId){
        Map<String, Object> hotelListById = hotelService.findHotelListById(hotelId);
        if (hotelListById != null){
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,hotelListById);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询房间类型
     * @param hotelId 酒店id
     * @param checkTime 入住时间
     * @param leaveTime 离店时间
     * @return Map<String,Object>
     */
    @GetMapping("/findRoomTypeById")
    public Object findRoomTypeById(@RequestParam(value = "hotelId")long hotelId, @RequestParam(value = "checkTime")String checkTime, @RequestParam(value = "leaveTime")String leaveTime ){
        List<Map<String, Object>> roomType = hotelService.findRoomTypeById(hotelId, checkTime, leaveTime);
        System.out.println(roomType);
        if (roomType != null){
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,roomType);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);
    }

    /**
     * 查询酒店pi
     * @param hotelId
     * @return
     */
    @GetMapping("/findAssessById")
    public Object findAssessById(@RequestParam(value = "hotelId") long hotelId) {
        List<Map<String, Object>>  assessList = hotelService.findAssessById(hotelId);
        if (assessList != null){
            return new Result("查询成功", ReturnResults.ACCESS_SUCCESS,assessList);
        }
        return new Result("查询失败", ReturnResults.ACCESS_FAILED);

    }
}
